package leetcode.list;

public class ReverseList {
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode pre = null;
        ListNode cur = head;
        ListNode next;

        while (cur != null) {
            next = cur.next;
            cur.next = pre;

            pre = cur;
            cur = next;
        }

        return pre;
    }

    public static void main(String[] args) {
        ListNode a = new ListNode(1);
        ListNode a2 = new ListNode(2);
        a.next = a2;
        ListNode a3 = new ListNode(3);
        a2.next = a3;
        ListNode a4 = new ListNode(4);
        a3.next = a4;
        ListNode a5 = new ListNode(5);
        a4.next = a5;

        ReverseList reverseList = new ReverseList();
        ListNode listNode = reverseList.reverseList(a);

        System.out.println("listNode = " + listNode);
    }
}
